package org.com.liurz.iresources.activiti.listener;

import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.com.liurz.iresources.activiti.controller.WorkFlowController;
import org.com.liurz.iresources.activiti.util.FlowConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * 全局监听器
 * @author liurz
 * @version V1.0
 * @Package org.com.liurz.iresources.activiti.listener
 * @date 2021/8/28 1:01
 * @Copyright © 2020-2028
 */
@Component
public class GlobeActivitiEventListener implements ActivitiEventListener, Serializable {
    private Logger logger = LoggerFactory.getLogger(GlobeActivitiEventListener.class);
    //事件及事件的处理器
    @Override
    public void onEvent(ActivitiEvent event) {
        logger.info("ProcessDefinitionId:"+event.getProcessDefinitionId());
        logger.info("ProcessInstanceId:"+event.getProcessInstanceId());
        logger.info("ExecutionId:"+event.getExecutionId());
        logger.info("ProcessDefinitionId:"+event.getType().name());
        switch (event.getType()) {
            case PROCESS_CANCELLED:
                logger.info(event.getType().name() + " ---- " + event.getProcessInstanceId());
                break;
            case PROCESS_COMPLETED:
                // 完成的时候，在从流程实例中获取process_result变量
                logger.info(event.getType().name() + " ---- " + event.getProcessInstanceId());
                //EngineServices engineServices = event.getEngineServices();
                //Object process_result = engineServices.getRuntimeService().getVariable(event.getExecutionId(), FlowConstant.PROCESSRESULT);
              /*  if(process_result == null || (Boolean) process_result) {
                    logger.info("流程正常完成："+event.getType().name() + " ---- " + event.getProcessInstanceId());
                } else {
                    // 驳回、不同意导致流程结束
                    logger.info("流程失败完成："+event.getType().name() + " ---- " + event.getProcessInstanceId());
                }*/
                break;
            case PROCESS_COMPLETED_WITH_ERROR_END_EVENT:
                logger.info(event.getType().name() + " ---- " + event.getProcessInstanceId());
                // 发生错误导致流程完成
                logger.info("流程异常完成："+event.getType().name() + " ---- " + event.getProcessInstanceId());
                break;
            case PROCESS_STARTED:
                break;

            default:
                break;
        }
    }

    @Override
    public boolean isFailOnException() {
   // TODO Auto-generated method stub

        return false;

    }

}